<template>
  <div class="picture-liked-page">
    <div class="page-container">
      <!-- 顶部搜索区域 -->
      <div class="search-section">
        <div class="search-header">
          <h1 class="page-title">
            <HeartOutlined class="title-icon" />
            喜欢的图片
          </h1>
          <p class="page-subtitle">搜索并管理您收藏的图片</p>
        </div>

        <div class="search-box">
          <a-input-search
            v-model:value="formState.searchText"
            @search="submitForm"
            placeholder="请输入图片搜索词"
            size="large"
            class="search-input"
          >
            <template #prefix>
              <SearchOutlined />
            </template>
            <template #enterButton>
              <a-button type="primary" :loading="loading">
                <template #icon><SearchOutlined /></template>
                搜索
              </a-button>
            </template>
          </a-input-search>
        </div>
      </div>

      <!-- 图片展示区域 -->
      <div class="content-section">
        <a-card
          class="picture-card"
          :loading="pictureVoList.length > 0 ? false : loading"
          :bodyStyle="{ padding: '24px' }"
        >
          <template #extra>
            <a-space>
              <a-tag color="blue">
                <template #icon><PictureOutlined /></template>
                共 {{ pictureVoList.length }} 张图片
              </a-tag>
              <a-button
                type="primary"
                @click="nextPage"
                :disabled="!hasMore"
                :loading="loadingMore"
              >
                <template #icon><DownOutlined /></template>
                加载更多
              </a-button>
            </a-space>
          </template>

          <div class="waterfall-container" ref="scrollContainer">
            <Waterfall
              :list="pictureVoList"
              :gutter="16"
              :width="300"
              :breakpoints="{
                1919: { rowPerView: 6 },
                1200: { rowPerView: 4 },
                800: { rowPerView: 3 },
                500: { rowPerView: 2 },
                0: { rowPerView: 1 },
              }"
              :backgroundColor="'#f5f7fa'"
            >
              <template #item="{ item }">
                <div class="picture-item">
                  <div class="picture-wrapper">
                    <LazyImg :url="item.url" v-if="item.url" class="picture-image" />
                  </div>
                  <a-tooltip placement="bottom">
                    <div class="picture-overlay">
                      <div
                        style="display: flex; justify-content: space-evenly; align-items: center"
                      >
                        <a-tooltip title="预览">
                          <a-button type="primary" shape="circle" @click="handlePreview(item)">
                            <template #icon><EyeOutlined /></template>
                          </a-button>
                        </a-tooltip>
                        <a-tooltip title="下载">
                          <a-button type="primary" shape="circle" @click="handleDownload(item)">
                            <template #icon><DownloadOutlined /></template>
                          </a-button>
                        </a-tooltip>
                      </div>
                    </div>
                  </a-tooltip>
                </div>
              </template>
            </Waterfall>
          </div>
        </a-card>
      </div>
    </div>

    <!-- 图片预览 -->
    <a-modal
      v-model:open="previewVisible"
      :footer="null"
      width="800px"
      class="preview-modal"
      @cancel="previewVisible = false"
    >
      <div class="preview-content" v-if="currentPreview">
        <img :src="currentPreview.url" class="preview-image" />
        <div class="preview-info">
          <h3>{{ currentPreview.introduction }}</h3>
          <p class="preview-time">收藏时间：{{ formatTime(currentPreview.createTime) }}</p>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { searchLikePictureUsingPost } from '@/api/pictureController.ts'
import { formatTime } from '@/utils'
import {
  DownloadOutlined,
  DownOutlined,
  EyeOutlined,
  HeartOutlined,
  PictureOutlined,
  SearchOutlined,
} from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { reactive, ref } from 'vue'
import { LazyImg, Waterfall } from 'vue-waterfall-plugin-next'
import 'vue-waterfall-plugin-next/dist/style.css'

// 定义分页参数接口
interface PageParams {
  current: number
  pageSize: number
}

// 定义搜索表单接口
interface SearchForm extends API.PictureSearchRequest, PageParams {
  searchText: string
}

const loading = ref<boolean>(false)
const loadingMore = ref<boolean>(false)
const hasMore = ref<boolean>(true)
const previewVisible = ref<boolean>(false)
const currentPreview = ref<API.PictureVO | null>(null)

// 定义表单
const formState = reactive<SearchForm>({
  searchText: '',
  current: 1,
  pageSize: 20,
})

// 图片列表
const pictureVoList = ref<API.PictureVO[]>([])

// 搜索提交
const submitForm = async () => {
  loading.value = true
  formState.current = 1
  const res = await searchLikePictureUsingPost({
    searchText: formState.searchText,
    current: formState.current,
    pageSize: formState.pageSize,
  })
  if (res.data.code === 0 && res.data.data) {
    pictureVoList.value = res.data.data
    hasMore.value = res.data.data.length === formState.pageSize
  }
  loading.value = false
}

// 加载更多
const nextPage = async () => {
  if (loadingMore.value || !hasMore.value) return
  loadingMore.value = true
  formState.current++
  const res = await searchLikePictureUsingPost({
    searchText: formState.searchText,
    current: formState.current,
    pageSize: formState.pageSize,
  })
  if (res.data.code === 0 && res.data.data) {
    pictureVoList.value = [...pictureVoList.value, ...res.data.data]
    hasMore.value = res.data.data.length === formState.pageSize
  }
  loadingMore.value = false
}

// 预览图片
const handlePreview = (item: API.PictureVO) => {
  currentPreview.value = item
  previewVisible.value = true
}

// 下载图片
import { saveAs } from 'file-saver'
const handleDownload = (item: API.PictureVO) => {
  if (!item.url) {
    message.error('图片地址无效')
    return
  }
  saveAs(item.url, item.introduction || '图片')
  message.success('开始下载')
}
</script>

<style scoped lang="scss">
.picture-liked-page {
  min-height: 100vh;
  background: #f5f7fa;
  padding: 24px;

  .page-container {
    max-width: 1400px;
    margin: 0 auto;
  }

  .search-section {
    background: #fff;
    padding: 32px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    margin-bottom: 24px;

    .search-header {
      margin-bottom: 24px;

      .page-title {
        font-size: 28px;
        font-weight: 600;
        color: #1f1f1f;
        margin: 0 0 8px;
        display: flex;
        align-items: center;
        gap: 12px;

        .title-icon {
          color: #ff4d4f;
        }
      }

      .page-subtitle {
        color: #666;
        font-size: 16px;
        margin: 0;
      }
    }

    .search-box {
      .search-input {
        :deep(.ant-input-affix-wrapper) {
          border-radius: 8px 0 0 8px;
        }

        :deep(.ant-input-search-button) {
          border-radius: 0 8px 8px 0;
        }
      }
    }
  }

  .content-section {
    .picture-card {
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);

      :deep(.ant-card-head) {
        border-bottom: 1px solid #f0f0f0;
        padding: 16px 24px;
      }
    }

    .waterfall-container {
      min-height: 400px;
    }

    .picture-item {
      background: #fff;
      border-radius: 8px;
      overflow: hidden;
      transition: all 0.3s ease;
      position: relative;

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

        .picture-overlay {
          opacity: 1;
          visibility: visible;
        }

        .picture-image {
          transform: scale(1.05);
        }
      }

      .picture-wrapper {
        position: relative;
        overflow: hidden;
        border-radius: 8px 8px 0 0;
        aspect-ratio: 1;
        width: 100%;

        .picture-image {
          width: 100%;
          height: 100%;
          object-fit: cover;
          display: block;
          transition: all 0.3s ease;
        }

        .picture-overlay {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: rgba(0, 0, 0, 0.6);
          display: flex;
          align-items: center;
          justify-content: center;
          opacity: 0;
          visibility: hidden;
          transition: all 0.3s ease;
          backdrop-filter: blur(2px);
          z-index: 1;

          .ant-btn {
            width: 44px;
            height: 44px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            transition: all 0.3s ease;
            background: rgba(255, 255, 255, 0.9);
            border: none;
            margin: 0 8px;
            z-index: 2;

            &:hover {
              transform: scale(1.1);
              background: #fff;
            }

            .anticon {
              color: #1890ff;
            }

            &.ant-btn-primary {
              background: #1890ff;
              border: none;

              .anticon {
                color: #fff;
              }

              &:hover {
                background: #40a9ff;
              }
            }
          }
        }
      }

      .picture-title {
        padding: 12px;
        margin: 0;
        color: #1f1f1f;
        font-size: 14px;
        line-height: 1.5;
      }
    }
  }
}

// 预览弹窗样式
.preview-modal {
  :deep(.ant-modal-content) {
    border-radius: 12px;
    overflow: hidden;
  }

  .preview-content {
    .preview-image {
      width: 100%;
      max-height: 600px;
      object-fit: contain;
    }

    .preview-info {
      padding: 16px;
      text-align: center;

      h3 {
        margin: 0 0 8px;
        color: #1f1f1f;
      }

      .preview-time {
        color: #666;
        margin: 0;
      }
    }
  }
}

// 响应式优化
@media screen and (max-width: 768px) {
  .picture-liked-page {
    padding: 16px;

    .search-section {
      padding: 24px;

      .search-header {
        .page-title {
          font-size: 24px;
        }
      }
    }
  }
}
</style>
